from flask import Blueprint, request

from static.connect.conn_mysql import Connect

from .access_token import Card

app_call_car = Blueprint('app_call_car', __name__)


@app_call_car.route('/callcar', methods=['POST'])
def call_car():
    info = request.form
    go_time = info.get('go_time')
    place_departure = info.get('place_departure')
    destination = info.get('destination')
    number_people = info.get('number_people')
    phone = info.get('phone')
    user_id = info.get('user_id')
    carpool_hitl = info.get('carpool_hitl')
    direction = info.get('direction')
    access_token = info.get('access_token')

    if not all([go_time, place_departure, destination,
                number_people, phone, user_id, carpool_hitl, direction]):

        return {
            'status': -1,
            'errorCode': 21,
            'errorMsg': '有参数值为空',
            'resultBody': None
        }
    with Card() as C:
        verify_result = C.access_tokens(access_token, user_id)
    if verify_result['status'] == -1:
        return verify_result
    with Connect() as c:
        insert_sql = '''
        INSERT INTO call_car(go_time, place_departure, 
        destination,number_people, 
        phone, user_id, carpool_hitl, direction)
        VALUES
        (%s,%s,%s,%s,%s,%s,%s,%s);
        '''
        result = c.insert(insert_sql, (go_time, place_departure, destination,
                                       number_people, phone, user_id, carpool_hitl, direction))
    return {
        'status': 1,
        'errorCode': None,
        'errorMsg': None,
        'resultBody': '提交成功'
    }
